package com.xtool.livewallpaper

import android.app.WallpaperManager
import android.content.BroadcastReceiver
import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.content.Intent.FLAG_ACTIVITY_NEW_TASK
import com.xtool.livewallpaper.transparent.TransparentWallpaperService
import com.xtool.livewallpaper.base.PrefsHelper
import com.xtool.livewallpaper.video.VideoWallpaperService

/**
 * @ClassName: WallpaperBootReceiver
 * @Description:
 * @Author: Haiyang
 * @Date: 2021/7/30 22:15
 */
class WallpaperBootReceiver : BroadcastReceiver() {
    companion object {
        const val TYPE_VIDEO = 0
        const val TYPE_CAMERA = 1
        const val TYPE_GIF = 2
    }

    override fun onReceive(context: Context?, intent: Intent?) {
        try {
            context ?: return
            if (!PrefsHelper.isAutoBoot) return
            if (intent?.action.equals(Intent.ACTION_BOOT_COMPLETED)) {
                val chooseIntent = Intent()
                chooseIntent.action = WallpaperManager.ACTION_CHANGE_LIVE_WALLPAPER
                val className = when (PrefsHelper.lastWallpaperType) {
                    TYPE_VIDEO -> VideoWallpaperService::class.java.canonicalName
                    TYPE_CAMERA -> TransparentWallpaperService::class.java.canonicalName
                    else -> return
                }
                chooseIntent.putExtra(
                    WallpaperManager.EXTRA_LIVE_WALLPAPER_COMPONENT,
                    ComponentName(context.packageName, className)
                )
                chooseIntent.`package` = context.packageName
                chooseIntent.flags = FLAG_ACTIVITY_NEW_TASK
                context.startService(chooseIntent)
            }
        } catch (e: Exception) {
        }
    }
}